var noClicksetting = {
    view:{
        selectedMulti: false
    },
    data: {
        simpleData: {
            enable: true,
            pIdKey: "pid"
        }
    },
    check:{
    	enable:true,
        chkStyle: "checkbox",
        chkboxType: { "Y": "", "N": "" }
    },
    callback: {
    	beforeCheck: ztreeBeforeCheck,
        onClick : zTreeOnClick
    }

};

var clicksetting = {
	    view:{
	        selectedMulti: false
	    },
	    data: {
	        simpleData: {
	            enable: true,
	            pIdKey: "pid"
	        }
	    },
	    check:{
	    	enable:true,
	        chkStyle: "checkbox",
			autoCheckTrigger: true
	    },
	    callback: {
	    	beforeCheck: ztreeBeforeCheck,
	        onClick : zTreeOnClick
	    }

	};

function ztreeBeforeCheck(treeId, treeNode){
	var zTree = $.fn.zTree.getZTreeObj(ulIdval),
		nodes = zTree.getCheckedNodes();
	if(!ifCheckedval){
		if(nodes.length > 0){
		    zTree.checkAllNodes(false);
		}
	}
	
	return true;
}

function zTreeOnClick(event, treeId, treeNode,clickFlag){
	
//    var zTree = $.fn.zTree.getZTreeObj(ulIdval);
//    var nodeObj = zTree.getNodeByTId(treeId);
//    zTree.checkNode(treeNode,treeNode.checked==true?false:true,false,true);
//    var nodes = zTree.getSelectedNodes();  
    
}

/* 
 * 展示设备类型SelectTree 
 * objLayer 由于都是由layer弹框调用，因此传入一个layer对象
 * ulId ztree ul标签的id
 * inputById 存放选中节点编号的文本框id
 * inputByName 存放选中节点名称的文本框id
 * divId 属性菜单的上级div的ID
 * ifChecked 是否多选 true多选  false单选
 * ifParentChecked true 显示父级多选框 false：不显示父级多选框
 * type 0：只显示部门  1:部门和员工一起显示
 * 
 * ifChecked:true,ifParentChecked:true,type:0 只有部门，支持多选
 * ifChecked:false,ifParentChecked:true,type:0 只有部门，支持单选
 * ifChecked:true,ifParentChecked:true,type:1 部门和员工，支持多选
 * ifChecked:false,ifParentChecked:true,type:1 部门和员工,支持单选(不建议使用)
 * ifChecked:true,ifParentChecked:false,type:1 部门和员工，部门没有多选框，员工支持多选
 * ifChecked:false,ifParentChecked:false,type:1 部门和员工，部门没有多选框，员工支持单选
 */  
var objLayers='';
var ulIdval='';
var inputByIdval='';
var inputByNameval='';
var ifCheckedval;
var divIdval='';
var typeval='';
function showDevTypeTree(objLayer,ulId,inputById,inputByName,divId,ifChecked,ifParentChecked,type){  
	ifCheckedval=ifChecked;
	objLayers=objLayer;
	ulIdval=ulId;
	inputByIdval=inputById;
	inputByNameval=inputByName;
	divIdval=divId;
	typeval=type;
	var checkedId='';
	if(objLayer!=null){
		checkedId=objLayers.find("#"+inputById+"").val();
	}else{
		checkedId=$("#"+inputById+"").val();
	}
	ajaxSer.deptOrEmpTree(checkedId,ifChecked,ifParentChecked,type,function(data){
		
		var deviceTypeNodes = [];  
		var obj=eval("("+data+")");
		var zNodes = obj.nodes;
		if(objLayers!=null){
			if(ifChecked){
				$.fn.zTree.init(objLayers.find("#"+ulId+""), clicksetting, zNodes);
			}else{
				$.fn.zTree.init(objLayers.find("#"+ulId+""), noClicksetting, zNodes);
			}
			
			getDevTypeObj(obj,deviceTypeNodes);  
	        var deptObj = objLayers.find("#"+inputByName+"");  
	        var deptOffset = objLayers.find("#"+inputByName+"").offset();  
	        objLayers.find('#' + divId +'').css({left:"0px", top:"34px", maxWidth:'100%', zIndex:'999999999'}).slideDown("fast");  
	        objLayers.find('#'+ulId+'').css({width:deptObj.outerWidth() - 12 + "px"});  
		}else{
			if(ifChecked){
				$.fn.zTree.init($("#"+ulId+""), clicksetting, zNodes);
			}else{
				$.fn.zTree.init($("#"+ulId+""), noClicksetting, zNodes);
			}
			
			getDevTypeObj(obj,deviceTypeNodes);  
	        var deptObj = $("#"+inputByName+"");  
	        var deptOffset = $("#"+inputByName+"").offset();  
	        $('#'+divId+'').css({left:"0px", top:"34px", maxWidth:'100%', zIndex:'999999999'}).slideDown("fast"); 
	        $('#'+ulId+'').css({width:deptObj.outerWidth() - 12 + "px"});  
		}
       
	});
}  
/** 
 * 设备类型 
 *  
 * @param {} dataObj 
 * @param {} treeNodes 
 */  
function getDevTypeObj(dataObj,treeNodes){  
    for (var i = 0; i < dataObj.length; i++) {  
        treeNodes.push({id:dataObj[i].id,pId:dataObj[i].pid,name:dataObj[i].name});  
        loadChildDevTypeObj(dataObj[i],treeNodes);  
    }  
}  
/** 
 * 查找子节点 
 *  
 * @param {} dataObj 
 * @param {} treeNodes 
 */  
function loadChildDevTypeObj(dataObj,treeNodes){  
    var childObj = dataObj.children;  
    for(var j = 0; j < childObj.length; j++){  
        treeNodes.push({id:childObj[j].id,pId:childObj[j].pid,name:childObj[j].name});  
        loadChildDevTypeObj(childObj[j],treeNodes);  
    }  
}  
/* 
 * Body鼠标按下事件回调函数 
 */  
function onBodyDownByDevType(event) {  
    
}  
/* 
 * 隐藏设备类型Tree 
 */  
function hideDeviceTypeMenu() {  
	debugger;
	
}

/*
*关闭
*/ 
function closeZtree(){
	if(objLayers!=null){
		objLayers.find('#' + divIdval +'').fadeOut("fast"); 
	}else{
		$('#' + divIdval +'').fadeOut("fast"); 
	}
}


/*
 * type 0 获取id 1获取empNo
 * resultType 0:返回勾选的名称 1：返回勾选的总数
 * 获取勾选的节点
 */
function returnCheckedNode(type,resultType){
	var zTree = $.fn.zTree.getZTreeObj(ulIdval);
    var obj = zTree.getCheckedNodes(true);
    if(ifCheckedval){
    	var objId='';
    	var objName='';
    	var count=0;
    	for (var i=0; i<obj.length; i++) {
    		
    		if(typeval=='0'){
    			if(obj[i].id.indexOf("dept")<0){
    				continue;
    			}
    		}else{
    			if(obj[i].id.indexOf("user")<0){
    				continue;
    			}
    		}
    		
    		if(objId==''){
    			 if(type=='0'){
    				 objId=obj[i].guid;  
    	    	 }else{
    	    		 objId=obj[i].empNo;
    	    	 }
    			 objName=obj[i].name;
    		}else{
    			if(type=='0'){
   				 	objId+=','+obj[i].guid;  
       	    	 }else{
       	    		 objId+=','+obj[i].empNo;
       	    	 }
    			 objName+=","+obj[i].name;
    		}
    		count++;
        }
    	if(objLayers!=null){
    		objLayers.find("#"+inputByIdval+"").val(objId);
    		if(resultType=='0'){
    			objLayers.find("#"+inputByNameval+"").val(objName); 
    		}else{
    			if(typeval=='0'){
    				objLayers.find("#"+inputByNameval+"").val('您一共选择了'+count+'个部门'); 
    			}else{
    				objLayers.find("#"+inputByNameval+"").val('您一共选择了'+count+'个员工'); 
    			}
    		}
    	}else{
    		$("#"+inputByIdval+"").val(objId);
    		if(resultType=='0'){
    			$("#"+inputByNameval+"").val(objName); 
    		}else{
    			if(typeval=='0'){
    				$("#"+inputByNameval+"").val('您一共选择了'+count+'个部门'); 
    			}else{
    				$("#"+inputByNameval+"").val('您一共选择了'+count+'个员'); 
    			}
    		}
    	}
    }else{
    	if(objLayers!=null){
    		if(type=='0'){
    			objLayers.find("#"+inputByIdval+"").val(obj[0].guid);
    		}else{
    			objLayers.find("#"+inputByIdval+"").val(obj[0].empNo);
    		}
       	 	objLayers.find("#"+inputByNameval+"").val(obj[0].name); 
        }else{
        	if(type=='0'){
    	    	$("#"+inputByIdval+"").val(obj[0].guid);  
        	}else{
        		$("#"+inputByIdval+"").val(obj[0].empNo);  
        	}
       	 
       	 	$("#"+inputByNameval+"").val(obj[0].name); 
       }
    }
    
    
    closeZtree();
}
